package com.elf.bluetoothtest.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class BluetoothHelp {

    public static boolean isSupport() {
        return BluetoothAdapter.getDefaultAdapter() != null;
    }

    public static BluetoothHelp getInstance(Context context) {
        if (isSupport()) {
            return new BluetoothHelp(context,BluetoothAdapter.getDefaultAdapter());
        }
        return null;
    }

    private final static int DISCOVERY_TYPE_ALL = 0;
    private final static int DISCOVERY_TYPE_CLASSIC = 1;
    private final static int DISCOVERY_TYPE_BLE = 2;

    private Context mContext;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothStatusListener mBluetoothStatusListener;
    private BroadcastReceiver mBroadcastReceiver;
    private int discoveryType = DISCOVERY_TYPE_ALL;

    private BluetoothHelp(Context context, BluetoothAdapter adapter) {
        mContext = context;
        mBluetoothAdapter = adapter;

        mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent == null) {
                    return;
                }
                String action = intent.getAction();

                if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                    int status = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    if (status == BluetoothAdapter.STATE_ON) {
                        if (mBluetoothStatusListener != null) {
                            mBluetoothStatusListener.onStateOn();
                        }
                    } else if (status == BluetoothAdapter.STATE_OFF) {
                        if (mBluetoothStatusListener != null) {
                            mBluetoothStatusListener.onStateOff();
                        }
                    }
                }

                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    //发现设备
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    if (device == null) {
                        return;
                    }
                    if (DISCOVERY_TYPE_BLE == discoveryType) {
                        if (device.getType() != BluetoothDevice.DEVICE_TYPE_LE) {
                            return;
                        }
                    } else if (DISCOVERY_TYPE_CLASSIC == discoveryType) {
                        if (device.getType() != BluetoothDevice.DEVICE_TYPE_CLASSIC) {
                            return;
                        }
                    }
                    short rssi = intent.getExtras().getShort(BluetoothDevice.EXTRA_RSSI);
                    if (mBluetoothStatusListener != null) {
                        mBluetoothStatusListener.onFoundDevice(device,rssi);
                    }
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    //扫描完成
                    if (mBluetoothStatusListener != null) {
                        mBluetoothStatusListener.onDiscoveryFinish();
                    }
                }
            }
        };

        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        context.registerReceiver(mBroadcastReceiver, filter);
    }

    public boolean isEnable() {
        return mBluetoothAdapter.isEnabled();
    }

    public void enable() {
        mBluetoothAdapter.enable();
    }

    public void disEnable() {
        mBluetoothAdapter.disable();
    }

    private void discovery(int type) {
        discoveryType = type;
        mBluetoothAdapter.startDiscovery();
    }

    public void discoveryAll() {
        discovery(DISCOVERY_TYPE_ALL);
    }

    public void discoveryClassic() {
        discovery(DISCOVERY_TYPE_CLASSIC);
    }

    public void discoveryBle() {
        discovery(DISCOVERY_TYPE_BLE);
    }

    public void stopDiscovery() {
        mBluetoothAdapter.cancelDiscovery();
    }

    public boolean isDiscovering() {
        return mBluetoothAdapter.isDiscovering();
    }

    public IBlueToothConnection getConnection(BluetoothDevice device) {
        if (device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
            return new BleBluetoothConnection(mContext, device);
        } else if (device.getType() == BluetoothDevice.DEVICE_TYPE_CLASSIC) {
            return new ClassicBluetoothConnection(mContext, device);
        }
        return null;
    }

    public void release() {
        try {
            mContext.unregisterReceiver(mBroadcastReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setBluetoothDiscoveryListener(BluetoothStatusListener listener) {
        mBluetoothStatusListener = listener;
    }

    /**
     * 蓝牙发现监听
     */
    public interface BluetoothStatusListener {

        /**
         * 发现设备
         *
         * @param device 设备对象
         */
        void onFoundDevice(BluetoothDevice device,short rssi);

        /**
         * 发现完成
         */
        void onDiscoveryFinish();

        /**
         * 蓝牙开启
         */
        void onStateOn();

        /**
         * 蓝牙关闭
         */
        void onStateOff();
    }
}
